package com.system.service.impl;

import com.common.enums.BaseResponseCode;
import com.common.utils.AjaxResult;
import com.common.utils.StringUtils;
import com.system.entity.SysRoleEntity;
import com.system.entity.SysUserEntity;
import com.system.entity.SysUserRoleEntity;
import com.system.entity.utilentity.RoleVo;
import com.system.mapper.SysUserRoleMapper;
import com.system.service.ISysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class SysUserRoleServiceImpl implements ISysUserRoleService {

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;


    /**
     * 数据添加
     *
     * @param sysUserRoleEntity
     */

    @Override
    public AjaxResult sysUserRoleAdd(SysUserRoleEntity sysUserRoleEntity) {
        int n = sysUserRoleMapper.sysUserRoleAdd(sysUserRoleEntity);
        return new AjaxResult(BaseResponseCode.ADD_SUCCESS.getCode(), BaseResponseCode.ADD_SUCCESS.getMsg());
    }

    /**
     * 删除与批量删除
     *
     * @param ids
     */
    @Override
    @Transactional
    public AjaxResult sysUserRoleDel(List<Integer> ids) {
        sysUserRoleMapper.sysUserRoleDel(ids);
        return new AjaxResult(BaseResponseCode.DELETE_SUCCESS.getCode(), BaseResponseCode.DELETE_SUCCESS.getMsg());
    }

    /**
     * 更新
     *
     * @param sysUserRoleEntity
     */
    @Override
    public AjaxResult sysUserRoleUpById(SysUserRoleEntity sysUserRoleEntity) {
        sysUserRoleMapper.sysUserRoleUpById(sysUserRoleEntity);
        return new AjaxResult(BaseResponseCode.UPDATE_SUCCESS.getCode(), BaseResponseCode.UPDATE_SUCCESS.getMsg());
    }

    /**
     * 查询单条数据
     */
    @Override
    public SysUserRoleEntity sysUserRoleGetById(String id) {
        return sysUserRoleMapper.sysUserRoleGetById(id);
    }


    /**
     * 查询数据库
     * 分配角色表
     *
     * @return
     */
    @Override
    public AjaxResult sysUserRoleSelectListByPage(SysUserRoleEntity sysUserRoleEntity) {
        Integer count = sysUserRoleMapper.sysUserRoleSelCount(sysUserRoleEntity);
        List<SysUserEntity> list = sysUserRoleMapper.sysUserRoleSelectListByPage(sysUserRoleEntity);
        return AjaxResult.data(count, list);
    }

    /**
     * 分配角色表 可添加表
     *
     * @param sysUserRoleEntity
     * @return
     */
    @Override
    public AjaxResult sysUserRoleSelectListByPageTo(SysUserRoleEntity sysUserRoleEntity) {
        int count = sysUserRoleMapper.sysUserRoleSelCountTo();
        List<SysUserEntity> list = sysUserRoleMapper.sysUserRoleSelectListByPageTo(sysUserRoleEntity);
        return AjaxResult.data(count, list);
    }

    /**
     * 添加分配的角色
     *
     * @param roleVo
     * @return
     */
    @Override
    public AjaxResult addRole(RoleVo roleVo) {
        sysUserRoleMapper.addRole(roleVo);
        return new AjaxResult(BaseResponseCode.ADD_SUCCESS.getCode(), BaseResponseCode.ADD_SUCCESS.getMsg());
    }

    /**
     * 查询用户的角色id
     *
     * @param userId
     * @return
     */
    @Override
    public Long sysUserRoleGetUserById(Long userId) {
        return sysUserRoleMapper.sysUserRoleGetUserById(userId);
    }

    /**
     * 获取角色权限字符
     *
     * @param userId
     * @return
     */
    @Override
    public Set<String> selectRoleKeys(Long userId) {
        List<SysRoleEntity> perms = sysUserRoleMapper.selectRolesByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (SysRoleEntity perm : perms) {
            if (StringUtils.isNotNull(perm)) {
                permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
            }
        }
        return permsSet;
    }
}